package org.jeecg.modules.water.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.water.entity.LwgyJointWater;
import org.jeecg.modules.water.entity.LwgyJointWaterConfiguration;
import org.jeecg.modules.water.service.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class SysnLoraWaterDateJob implements Job {


    @Autowired
    private ILwgyJointWaterService lwgyJointWaterService;
    @Autowired
    private ILwgyJointWaterConfigurationService lwgyJointWaterConfigurationService;
    @Autowired
    private ILwgyRelaxConnectionService lwgyRelaxConnectionService;

    //水控-定时器
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取前一天日期
        LocalDate yesterday = today.minusDays(1);

        // 获取前一天的凌晨时间（00:00:00）的字符串表示
        String yesterdayStartString = yesterday.atTime(LocalTime.MIN).format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println("前一天凌晨时间：" + yesterdayStartString);
        // 获取前一天的晚上时间（23:59:59）的字符串表示
        String yesterdayEndString = yesterday.atTime(LocalTime.MAX).format(DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println("前一天晚上时间：" + yesterdayEndString);
        lwgyRelaxConnectionService.BatchReadDeviceInformation();

        //获取所有房间的设备通信码
        QueryWrapper<LwgyJointWaterConfiguration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_type",1);//查询所有lora表
        List<LwgyJointWaterConfiguration> ljwc  = lwgyJointWaterConfigurationService.list(queryWrapper);
        System.out.println("ljwc:"+ljwc);


        //获取最新的抄表记录更新之设备列表
        QueryWrapper<LwgyJointWater> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.like("create_time",today);//获取今天的抄表数据
        List<LwgyJointWater>  list = lwgyJointWaterService.list(queryWrapper2);
        System.out.println("list:"+list);

        for(LwgyJointWaterConfiguration item1:ljwc){
            String sb=item1.getDeviceNumber();
            System.out.println("设备管理中的电池状态"+item1.getBatteryStatus());
            for(LwgyJointWater item2:list){
                String sb2 = item2.getIspid();
                if(sb.equals(sb2)){
                    item1.setBatteryStatus(item2.getBattery());
                    item1.setReading(item2.getCurnum());
                    System.out.println("抄表的电池状态："+item2.getBattery());

                }
            }
        }
        lwgyJointWaterConfigurationService.updateBatchById(ljwc);
    }
}
